<?php
declare(strict_types=1);

namespace WebApp\lib;

use WebApp\lib\app\AbstractApp;
use WebApp\lib\app\CallApp;

/**
 * Class Controller
 * @package WebApp\lib
 */
abstract class Controller
{
    /**
     * @var AbstractApp
     */
    protected $app;
    /**
     * Controller constructor.
     * @param AbstractApp $app
     */
    public function __construct($app)
    {
        if (is_array($app)) {
            $this->app = new CallApp($app);
            $this->init();
        } else {
            $this->app = $app;
        }
    }

    abstract public function init(): void;
    abstract public function end(): void;

    /**
     * @param $param
     * @throws \Exception
     */
    protected function dump($param)
    {
        ob_start();
        echo '<pre>';
        var_dump($param);
        $res = ob_get_contents();
        ob_clean();
        $this->app->getResponse()->end($res);
    }


    /**
     * @return app\ReqProxy
     */
    public function getRequest()
    {
        return $this->app->getRequest();
    }
}
